/**
  ******************************************************************************
  * @file    main.c
  * @author  AE Team
  * @version 1.0.3
  * @date    2024-05-28
  * @brief   本样例演示采用 SoftEE 库，对软EE进行读写操作
  *          通过算法，将FLASH的空间模拟EEPROM
  *          采用了空间换寿命的算法以大幅提高软EE的擦写寿命
  *          样例成功执行后LED(PA03)会进行闪烁，否则会保持常亮
  *
  * @note    硬件平台：SYM32L010_StartKit_V1.0
  *          接线说明：请取消Reset And Run，以防止下载完成之后自动运行
  *                    短接J28
  *
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2024 SIYIMicro.
  * All rights reserved.</center></h2>
  *
  *
  ******************************************************************************
  */


/******************************************************************************/
/* Include files                                                              */
/******************************************************************************/
#include "main.h"
#include "sym32l010_hal_sysctrl.h"
#include "sym32l010_hal_systick.h"
#include "sym32l010_hal_gpio.h"
#include "sym32l010_hal_flash.h"
#include "sym32l010_hal_SoftEE.h"
#include "sym32l010_startkit_board.h"


/******************************************************************************/
/* Local pre-processor symbols/macros ('#define')                             */
/******************************************************************************/

/******************************************************************************/
/* Global variable definitions (declared in header file with 'extern')        */
/******************************************************************************/

/******************************************************************************/
/* Local type definitions ('typedef')                                         */
/******************************************************************************/

/******************************************************************************/
/* Local function prototypes ('static')                                       */
/******************************************************************************/

/******************************************************************************/
/* Local variable definitions ('static')                                      */
/******************************************************************************/

/******************************************************************************/
/* Local pre-processor symbols/macros ('#define')                             */
/******************************************************************************/

/******************************************************************************/
/* Function implementation - global ('extern') and local ('static')           */
/******************************************************************************/


/*******************************************************************************
 ** \brief  Main function of project
 **
 ** \return uint32_t return value, if needed
 **
 **
 ******************************************************************************/
int32_t main(void)
{
    // 使能外设时钟
    HAL_SYSCTRL_CRC_CLK_ENABLE();
    HAL_SYSCTRL_FLASH_CLK_ENABLE();

    // 初始化SysTick
    HAL_InitTick(4000000);

    // 初始化板载LED
    Bsp_Led_Init();

    // 初始化SoftEE
    SOFTEE_Init();

    //------------------------------------------------------------
    // 准备待写入的数据
    uint8_t WrBuf[SOFTEE_SIZE], RdBuf[SOFTEE_SIZE];
    for (uint8_t i=0; i<SOFTEE_SIZE; i++)
    {
        WrBuf[i] = i * 117;
    }

    // 写入SoftEE
    SOFTEE_Write(0, WrBuf, 8);
    SOFTEE_Write(8, WrBuf + 8, SOFTEE_SIZE - 8);

    // 读取SoftEE
    SOFTEE_Read(0, RdBuf, SOFTEE_SIZE);

    // 检验SoftEE
    for (uint8_t i=0; i<SOFTEE_SIZE; i++)
    {
        if (WrBuf[i] != RdBuf[i])
        {
            while (1);
        }
    }

    //------------------------------------------------------------
    // 结束
    while (1)
    {
        BSP_LED_TOG();
        HAL_Delay(100);
    }
}


/*******************************************************************************
 ** EOF (not truncated)
 ******************************************************************************/
#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
    /* USER CODE BEGIN */
    /* User can add his own implementation to report the file name and line number,
       tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
    /* USER CODE END */
}
#endif /* USE_FULL_ASSERT */


/************************ (C) COPYRIGHT SIYIMicro *****END OF FILE*************/
